package cn.lysj.framework.customer.service;

import cn.lysj.framework.base.BaseService;
import cn.lysj.framework.common.utils.random.CustomTimeType;
import cn.lysj.framework.common.utils.random.CustomTimeUtil;
import cn.lysj.framework.common.utils.web.ParamUtil;
import cn.lysj.framework.common.utils.web.Resp;
import cn.lysj.framework.coupon.service.BirthdayCouponConfigService;
import cn.lysj.framework.coupon.service.NewVipCouponConfigService;
import cn.lysj.framework.coupon.vo.BirthdayCouponToUserVo;
import cn.lysj.framework.coupon.vo.NewVipCouponToUserVo;
import cn.lysj.framework.customer.domain.Customer;
import cn.lysj.framework.customer.domain.CustomerInfo;
import cn.lysj.framework.customer.domain.CustomerVip;
import cn.lysj.framework.customer.repository.CustomerInfoRepository;
import cn.lysj.framework.customer.repository.CustomerRepository;
import cn.lysj.framework.customer.repository.CustomerVipRepository;
import cn.lysj.framework.customer.vo.CustomerVipInfoVo;
import cn.lysj.framework.store.domain.Store;
import cn.lysj.framework.store.service.StoreService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import javax.transaction.Transactional;
import javax.annotation.Resource;
import java.util.*;

/**
 * @author zwj
 * @description VIP用户服务层
 * @create 2019-01-15 15:43:50
 **/
@Slf4j
@Service
@Transactional
public class CustomerVipService implements BaseService<CustomerVip> {

    @Resource
    private CustomerVipRepository customerVipRepository;

    @Resource
    private NewVipCouponConfigService newVipCouponConfigService;

    @Resource
    private BirthdayCouponConfigService birthdayCouponConfigService;

    @Resource
    private CustomerRepository customerRepository;

    @Resource
    private CustomerInfoRepository customerInfoRepository;

    @Resource
    private StoreService storeService;

    @Override
    public CustomerVipRepository getRepository() {
        return customerVipRepository;
    }

    /**
     * @author ZhangWenJian
     * @date 2019/1/16 11:44
     * @title buildCustomerVip
     * @param storeId
     * @param customerInfoId
     * @return cn.lysj.framework.common.utils.web.Resp
     * @description 建立会员关系
     */
    public CustomerVip  buildCustomerVip(String storeId,String customerInfoId){
        CustomerVip customerVip = customerVipRepository.findByStoreIdAndCustomerInfoId(storeId,customerInfoId);
        if(customerVip == null){
            customerVip = new CustomerVip(storeId,customerInfoId);
            customerVip = customerVipRepository.saveAndFlush(customerVip);
        }
        return customerVip;
    }
    /**
     * @author ZhangWenJian
     * @date 2019/1/16 14:22
     * @title judgeCustomerVip
     * @param storeId
     * @param customerId
     * @return cn.lysj.framework.common.utils.web.Resp
     * @description 判断用户是不是vip用户
     */
    public Resp judgeCustomerVip(String storeId,String customerId){
        Map<String,Object> resultMap = new HashMap<>();
        CustomerVipInfoVo customerVipInfoVo = customerVipRepository.judgeCustomerVip(storeId,customerId);
        Integer customerVipType;
        Integer customerInfoType;
        if(customerVipInfoVo == null){
            customerVipType = 1;
            customerInfoType = 1;
            CustomerInfo customerInfo = new CustomerInfo();
            Customer customer = customerRepository.findOne(customerId);
            if(!ParamUtil.isBlank(customer.getCustomerInfoId())){
                customerInfo = customerInfoRepository.findOne(customer.getCustomerInfoId());
                if(customerInfo != null){
                    customerInfoType = 2;
                }
            }
            resultMap.put("customerVipInfo",customerInfo);
        }
        else{
            customerVipType = 2;
            customerInfoType = 2;
            resultMap.put("customerVipInfo",customerVipInfoVo);
        }
        //前端数据返回 birthdayCouponToUserVos：红包数据数组  customerVipType：vip类型 1.非vip 2.是vip
        resultMap.put("customerInfoType",customerInfoType);
        resultMap.put("customerVipType",customerVipType);
        return new Resp(resultMap,"获取用户会员信息成功!");
    }

    /**
     * @author ZhangWenJian
     * @date 2019/1/18 11:41
     * @title getCustomerVipData
     * @param storeId
     * @param customerId
     * @return cn.lysj.framework.common.utils.web.Resp
     * @description 获取用户VIP数据 以及可以领取的红包
     */
    public Resp getCustomerVipDate(String storeId,String customerId){
        CustomerVipInfoVo customerVipInfoVo = customerVipRepository.judgeCustomerVip(storeId,customerId);
        Map<String,Object> resultMap = new HashMap<>();
        Integer customerVipType = 2;
        List<BirthdayCouponToUserVo> birthdayCouponToUserVos = new ArrayList<>();
        List<NewVipCouponToUserVo> newVipCouponToUserVos = new ArrayList<>();
        if(customerVipInfoVo == null){
            customerVipType = 1;
        }
        else{
            //用户是vip执行 红包模块 获取当前用户可以领取的生日红包
            Store store = storeService.getRepository().findOne(storeId);
            if(store.getAdvanceProvideTime() != null && !CustomTimeUtil.getSysYear().equals(customerVipInfoVo.getBirthdayGetCouponRecord())){
                Date beginTime = CustomTimeUtil.changeBirthday(CustomTimeUtil.getSkewingTimeInDay(new Date(),0, CustomTimeType.BEGIN),customerVipInfoVo.getBirthday());
                Date endTime = CustomTimeUtil.getSkewingTimeInDay(beginTime,store.getAdvanceProvideTime(), CustomTimeType.END);
                if(customerVipInfoVo.getBirthday().compareTo(beginTime)>=0 && customerVipInfoVo.getBirthday().compareTo(endTime)<=0){
                    birthdayCouponToUserVos = birthdayCouponConfigService.findBirthdayCouponToUserVo(storeId);
                }
            }
            //vip红包
            if(ParamUtil.isBlank(customerVipInfoVo.getNewVipGetCouponRecord())){
                newVipCouponToUserVos = newVipCouponConfigService.findNewVipCouponToUserVo(storeId);
            }
        }
        //前端数据返回 birthdayCouponToUserVos：红包数据数组 customerVipType：vip类型 1.非vip 2.是vip
        resultMap.put("birthdayCouponToUserVos",birthdayCouponToUserVos);
        resultMap.put("newVipCouponToUserVos",newVipCouponToUserVos);
        resultMap.put("customerVipType",customerVipType);

        return new Resp(resultMap,"获取用户会员信息成功!");
    }



}
